#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QDebug>
#include <QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->MuiscImageBox->installEventFilter(this);

}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->MuiscImageBox)
    {   //  鼠标进入             鼠标事件
        if(QEvent::Enter == event->type())
        {
            // 添加图片上移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->MuiscImageBox,"geometry");
            animation->setDuration(200);                                                                   // 持续时间
            animation->setStartValue(QRect(9,9,ui->MuiscImageBox->width(),ui->MuiscImageBox->height()));   // 起始状态
            animation->setEndValue(QRect(9,0,ui->MuiscImageBox->width(),ui->MuiscImageBox->height()));     // 结束状态
            animation->start();                                                                            // 启动动画
            // 捕获动画结束信号，释放animation
            connect(animation,&QPropertyAnimation::finished,this,[=](){delete animation;});

        }//       鼠标离开
        else if(QEvent::Leave == event->type())
        {
            // 添加图片复位动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->MuiscImageBox,"geometry");
            animation->setDuration(200);                                                                   // 持续时间
            animation->setStartValue(QRect(9,0,ui->MuiscImageBox->width(),ui->MuiscImageBox->height()));   // 起始状态
            animation->setEndValue(QRect(9,9,ui->MuiscImageBox->width(),ui->MuiscImageBox->height()));     // 结束状态
            animation->start();                                                                            // 启动动画
            // 捕获动画结束信号，释放animation
            connect(animation,&QPropertyAnimation::finished,this,[=](){delete animation;});
        }
        return true;
    }
    // 其他事件交给系统自动处理
    return QObject::eventFilter(watched,event);
}

void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString &imagePath)
{
    QString style = "background-image:url("+imagePath+");";
    ui->recMuiscImage->setStyleSheet(style);
}
